package com.fan.shop.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.fan.shop.domain.StockReqLine;
import com.fan.shop.domain.StockReqLineId;

public class StockReqLineDaoImpl implements StockReqLineDao {
	private SessionFactory sessionFactory;

	/**
	 * Get Hibernate Session Factory
	 * 
	 * @return SessionFactory - Hibernate Session Factory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * Set Hibernate Session Factory
	 * 
	 * @param SessionFactory
	 *            - Hibernate Session Factory
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public StockReqLine getStockReqLine(String requisitionNo, String itemCode) {
		Session session = sessionFactory.getCurrentSession();
		StockReqLineId stockReqLineId = new StockReqLineId(requisitionNo,
				itemCode);
		StockReqLine stockReqLine = (StockReqLine) session.get(
				StockReqLine.class, stockReqLineId);

		return stockReqLine;
	}

	@SuppressWarnings("unchecked")
	public List<StockReqLine> getAllStockReqLines() {
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(StockReqLine.class);
		List<StockReqLine> list = criteria.list();
		return list;
	}

	public int getSize() {
		return getAllStockReqLines().size();
	}

	public void addStockReqLine(StockReqLine stockReqLine) {
		Session session = sessionFactory.getCurrentSession();
		session.save(stockReqLine);

	}

	public void updateStockReqLine(StockReqLine stockReqLine) {
		Session session = sessionFactory.getCurrentSession();
		session.update(stockReqLine);

	}

	public void deleteStockReqLine(StockReqLine stockReqLine) {
		Session session = sessionFactory.getCurrentSession();
		session.delete(stockReqLine);

	}

	public boolean hasStockReqLine(String requisitionNo, String itemCode) {
		if (getStockReqLine(requisitionNo, itemCode) == null) {
			return false;
		} else {
			return true;
		}
	}

	@SuppressWarnings("unchecked")
	public List<String> getColumnNames() {
		List<String> columns = sessionFactory
				.getCurrentSession()
				.createSQLQuery(
						"SELECT COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'STOCK_REQ_LINE'")
				.list();

		return columns;
	}

}
